package com.xwb.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xwb.domain.Work;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
//private String competitionId;
//private String teamNumber;
//private String preliminaryType;
//private String preliminaryWork;
//private LocalDateTime preliminarySubmitDate;
//private String preliminaryMark;
//private String finalType;
//private String finalWork;
//private LocalDateTime finalSubmitDate;
//private String finalMark;

@Mapper
public interface WorkDao extends BaseMapper<Work> {

    @Insert("insert into work (competition_id, team_number, preliminary_type, preliminary_work, " +
            "preliminary_submitDate, preliminary_mark, final_type, final_work, " +
            "final_submitDate, final_mark) values " +
            "(#{competitionId},#{teamNumber},#{preliminaryType}," +
            "#{preliminaryWork},#{preliminarySubmitDate}," +
            "#{preliminaryMark},#{finalType},#{finalWork},#{finalSubmitDate},#{finalMark}) ")
    void save(Work work);

    @Select("select * from work")
    List<Work> showAllWork();

    /**
     * 根据比赛编号展示出该比赛下的作品
     */
    @Select("select * from work where competition_id = #{competitionId}")
    List<Work> showAllWorkByCompetitionId(String competitionId);

    /**
     * 根据初赛成绩，和比赛规则得出进入决赛的名单
     * enteringFinal = in 则进入决赛
     */
    @Update("UPDATE WORK w\n" +
            "JOIN (\n" +
            "    SELECT team_number\n" +
            "    FROM WORK\n" +
            "    ORDER BY CAST(preliminary_mark AS DECIMAL) DESC\n" +
            "    LIMIT #{enteringRank}\n" +
            ") top ON w.team_number = top.team_number\n" +
            "SET w.entering_final = 'in';")
    void updateFinaList(Integer enteringRank);

    /**
     * 将全部状态先设置成out
     */
    @Update("UPDATE work\n" +
            "SET entering_final = 'out'\n")
    void updateEliminator();

    /**
     * 将进入决赛的状态设置为null
     */
    @Update("UPDATE work\n" +
            "SET entering_final = NULL;\n")
    void setEnteringFinalNull();

    /**
     * 查询进入决赛的状态
     */
    @Select("select entering_final from work")
    List<String> selectEnteringFinal();


    /**
     * 根据entering_final展示出进入决赛的作品
     * In 表示进入决赛
     */
    @Select("select * from work where entering_final = 'in'")
    List<Work> showFinalist();



    /**
     * 决赛：
     * 在work表里根据决赛成绩进行排名
     */
    @Select("SELECT * FROM work WHERE entering_final = 'in' " +
            "ORDER BY CAST(final_mark AS DECIMAL) DESC;")
    List<Work> getFinalRank();


}
